#include "include.h"
#include "api.h"

#define BT_PROFILE          (PROF_A2DP*BT_A2DP_EN) | (PROF_HFP*BT_HFP_EN) | (PROF_SPP*BT_SPP_EN) | (PROF_HID*BT_HID_EN)
#define BT_CODEC            (CODEC_SBC) | (CODEC_MSBC * BT_HFP_MSBC_EN) | (CODEC_PLC * BT_PLC_EN)
#define HFP_FEATURE         (HFP_BAT_REPORT*BT_HFP_BAT_REPORT_EN) | (HFP_INBAND_RING_RONE*BT_HFP_PLAY_IOS_RING_EN) | (HFP_CALL_PRIVATE*BT_HFP_CALL_PRIVATE_FORCE_EN) | (HFP_RING_NUMBER_EN*BT_HFP_RING_NUMBER_EN)

#if BT_SUPPORT_IOS_FAST_STATUS && BT_BACKSTAGE_EN  &&  !BT_BACKSTAGE_PLAY_DETECT_EN
#define A2DP_FEATURE        (A2DP_VOL_CTRL*BT_A2DP_VOL_CTRL_EN) | (A2DP_RESTORE_PLAYING) | A2DP_IOS_PLAY_STATUS
#else
//#define A2DP_FEATURE        (A2DP_VOL_CTRL*BT_A2DP_VOL_CTRL_EN) | (A2DP_RESTORE_PLAYING) | (A2DP_AVDTP_DELAY_REPORT*BT_AVDTP_DELAY_REPORT_EN)
#define A2DP_FEATURE        (A2DP_VOL_CTRL*BT_A2DP_VOL_CTRL_EN) |  (A2DP_AVDTP_DELAY_REPORT*BT_AVDTP_DELAY_REPORT_EN)

#endif

///baseband
uint8_t cfg_bt_rf_def_txpwr         = 0;        //降低预置参数RF发射功率，单位1dbm
uint8_t cfg_bt_page_inq_txpwr       = 0;        //降低回连和搜索RF发射功率，单位2dbm

///stack
uint8_t cfg_bt_work_mode            = WORK_MODE;
uint8_t cfg_bt_max_acl_link         = BT_2ACL_EN+1;

bool cfg_bt_dual_mode               = BT_DUAL_MODE_EN;
bool cfg_bt_tws_mode                = BT_TWS_EN;
bool cfg_bt_scan_ctrl_mode          = BT_DISCOVER_CTRL_EN;
bool cfg_bt_simple_pair_mode        = BT_SIMPLE_PAIR_EN;
bool cfg_bt_disconnect_a2dp_playing = false;    //一拖二时，只断开当前播放的连接设备

uint16_t cfg_bt_support_profile     = BT_PROFILE;
uint16_t cfg_bt_support_codec       = BT_CODEC;

uint8_t cfg_bt_a2dp_feature         = A2DP_FEATURE;
uint8_t cfg_bt_hfp_feature          = HFP_FEATURE;
uint8_t cfg_bt_hid_type             = BT_HID_TYPE;

uint16_t cfg_bt_def_connect_times   = BT_POWER_UP_RECONNECT_TIMES;      //默认回连重试次数
uint16_t cfg_bt_sup_to_connect_times = 8;//BT_TIME_OUT_RECONNECT_TIMES;     //超时断线回连重试次数

//uint16_t cfg_bt_sup_to_connect_interval = 2000;                      //一直回连时,连一段时间空闲一段时间让手机能回连(默认空闲)
///app
const char bt_local_name[] = BT_NAME_DEFAULT;
const char bt_local_addr[6] = {0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa};



//自定义蓝牙类别图标，根据需要选择
u32 bt_get_class_of_device(void)
{
#if BT_HID_TYPE == 2
    return 0x0025c0;    //游戏手柄，GamePad
#else
//    return 0x002540;    //键盘图标，Android带显示电量，IOS不带电量显示。全部IOS均可连接HID拍照。
    return 0x240418;    //耳机图标，Android和IOS均带电量显示。IOS9之前的机型无法连接HID拍照。（默认使用）
#endif
}

//PIN配对码（最多16个字符），默认"0000"
//#if !BT_SIMPLE_PAIR_EN
//const char *bt_get_pin_code(void)
//{
//    return "0000";
//}
//#endif

//回连间隔（N+3000）mS，间隔越大，下一次回连越慢，更容易被其他手机搜索连接，N应大于等于2000
//u32 bt_get_conn_fail_delay(void)
//{
//    return 2000;
//}

//是否需要一直回连手机
//bool bt_is_always_reconn(void)
//{
//    return false;
//}

////按地址发起连接需打开此函数并填入BT地址，返回0地址无效，返回1地址有效
u8 addr_buf[6];
uint8_t bt_get_connect_addr(uint8_t *bd_addr)
{
   u8 *addr;
   addr = addr_buf;
   memcpy(bd_addr,addr,6);
   return 1;
}

///蓝牙连接限制名字连接，只连当前名字返回1，不连当前名字返回2，不做限制返回0
//u8 bt_connect_limit_name(void)
//{
//    return 0;
//}

const char *bt_get_local_name(void)
{
#if 1
#if FUNC_BTHID_EN
    if (is_bthid_mode()) {
        return xcfg_cb.bthid_name;
    }
#endif // FUNC_BTHID_EN
    return xcfg_cb.bt_name;
#else
    return bt_local_name;
#endif // 1

}
const bool cfg_bt_sniff_mode_dis   = true;  //true: 关闭蓝牙sniff功能

#if BT_TWS_EN
void bt_save_master_addr(u8 *addr)
{
    if (bt_tws_is_auto_switch()) {
        cm_write8(PARAM_MASTER_ADDR_VALID, 1);
        cm_write(addr, PARAM_MASTER_ADDR, 6);
        cm_sync();
    }
}

bool bt_get_master_addr(u8 *addr)
{
    if (bt_tws_is_auto_switch() && cm_read8(PARAM_MASTER_ADDR_VALID)) {
        cm_read(addr, PARAM_MASTER_ADDR, 6);
        return true;
    }
    return false;
}
#endif

void bt_get_local_bd_addr(u8 *addr)
{
#if BT_TWS_EN
    if (bt_get_master_addr(addr)) {
        return;
    }
#endif
#if BT_LOCAL_ADDR
    param_random_key_read(&addr[2]);
    addr[0] = 0x41;
    addr[1] = 0x42;
#else
    memcpy(addr, xcfg_cb.bt_addr, 6);
#endif
#if FUNC_BTHID_EN
    if (is_bthid_mode()) {
        addr[5] ^= BIT(0);
    }
#endif // FUNC_BTHID_EN
}

u32 bt_get_rand_seed(void)
{
    return sys_cb.rand_seed;
}

void bt_get_link_info(void *buf, u16 addr, u16 size)
{
    //printf("bt_read: %04x,%04x, %08lx\n", addr, size, BT_CM_PAGE(addr));
#if FUNC_BTHID_EN
    if (is_bthid_mode()) {
        cm_read(buf, BTHID_CM_PAGE(addr), size);
    } else
#endif // FUNC_BTHID_EN
    {
        cm_read(buf, BT_CM_PAGE(addr), size);
    }
    //print_r(buf, size);
}

void bt_put_link_info(void *buf, u16 addr, u16 size)
{
    //printf("bt_write: %04x,%04x, %08lx\n", addr, size, BT_CM_PAGE(addr));
    //print_r(buf, size);
#if FUNC_BTHID_EN
    if (is_bthid_mode()) {
        cm_write(buf, BTHID_CM_PAGE(addr), size);
    } else
#endif // FUNC_BTHID_EN
    {
        cm_write(buf, BT_CM_PAGE(addr), size);
    }
}

void bt_get_ext_link_info(void *buf, u16 addr, u16 size)
{
    //printf("bt_read: %04x,%04x, %08lx\n", addr, size, BT_CM_PAGE(addr));
    cm_read(buf, EXT_CM_PAGE(addr), size);
    //print_r(buf, size);
}

void bt_put_ext_link_info(void *buf, u16 addr, u16 size)
{
    //printf("bt_write: %04x,%04x, %08lx\n", addr, size, BT_CM_PAGE(addr));
    //print_r(buf, size);
    cm_write(buf, EXT_CM_PAGE(addr), size);
}

void bt_sync_link_info(void)
{
    cm_sync();
}

#if BT_FCC_TEST_EN
ALIGNED(4)
u8 huart_buffer[512];

void huart_init(void)
{
    huart_init_do(HUART_TR_PB3, HUART_TR_PB3, 1500000, huart_buffer, 512);
}
#endif
